package com.lovo.psc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lovo.psc.Uto.BookBean;
import com.lovo.psc.dto.PriceDto;
import com.lovo.psc.entity.Purchase;
import com.lovo.psc.entity.SupplierPurchase;
import com.lovo.psc.entity.Triumph;
import com.lovo.psc.entity.UserBean;
import com.lovo.psc.mapper.SupplierPurchaseMapper;
import com.lovo.psc.service.ISupplierPurchaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.psc.service.ITriumphService;
import com.lovo.psc.service.IUserService;
import com.lovo.psc.service.IVieBookService;
import com.lovo.util.MQUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.PipedWriter;
import java.util.List;

/**
 * <p>
 *  服务实现类 采购报价
 * </p>
 *
 * @author lovo
 * @since 2022-07-08
 */
@Service
public class SupplierPurchaseServiceImpl extends ServiceImpl<SupplierPurchaseMapper, SupplierPurchase> implements ISupplierPurchaseService {
    //采购书
    @Autowired
    private IVieBookService iVieBookService;
    //经销商
    @Autowired
    private IUserService iUserService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    private SupplierPurchaseMapper supplierPurchaseMapper;

    @Autowired
    private ITriumphService iTriumphService;




    @Override
    public void addBookPrice(SupplierPurchase supplierPurchase) {
        Purchase purchase = iVieBookService.findPurchaseById(supplierPurchase.getFkmarkbookid());

        if (0  == supplierPurchase.getQuote()){
            supplierPurchase.setState("弃标");
            super.save(supplierPurchase);

            return;
        }


        super.save(supplierPurchase);


        UserBean userBean = iUserService.findSuppById(purchase.getFkuserId());

        PriceDto priceDto = new PriceDto(userBean.getNumber(),userBean.getName(),supplierPurchase.getState(),
                supplierPurchase.getQuote()+"",supplierPurchase.getDetails(),purchase.getNumber(),purchase.getCommoditynum()+"");

        rabbitTemplate.convertAndSend(MQUtil.priceExchangeOrder,MQUtil.priceKey,priceDto);
    }



    public List<SupplierPurchase> findListSupplierPurchase(String number){
        QueryWrapper qw = new QueryWrapper();
        qw.eq("commodId",number);
        return super.list(qw);
    }

    @Override
    public void upDateNumStateBy(Integer BookNumber,Integer supplierNumber) {
        supplierPurchaseMapper.upDateNumStateBy(BookNumber,supplierNumber);
        supplierPurchaseMapper.upDateNumState(BookNumber,supplierNumber);
        //添加标书
        iTriumphService.addTriumph(this.findBookByTriumphSupplierNumber(BookNumber,supplierNumber));
    }

    @Override
    public void upDateNumState(Integer BookNumber, Integer supplierNumber) {
        supplierPurchaseMapper.upDateNumState(BookNumber,supplierNumber);
    }

    @Override
    public void del(Integer BookNumber) {
        supplierPurchaseMapper.del(BookNumber);
        int[] fk = supplierPurchaseMapper.findFK();
        for (int i = 0; i < fk.length; i++) {
            SupplierPurchase supplierPurchase = new SupplierPurchase();
            supplierPurchase.setState("废弃");
            super.save(supplierPurchase);
        }
    }

    @Override
    public Triumph findBookByTriumphSupplierNumber(Integer BookNumber, Integer supplierNumber) {
        return supplierPurchaseMapper.findBookByTriumphSupplierNumber(BookNumber,supplierNumber);
    }
}
